package com.jbp.controller;

import com.jbp.model.Bbs;
import com.jbp.service.BbsService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 留言板(Bbs)表控制层
 * @author cc
 * @since 2020-08-28 18:11:58
 */
@Api(tags ="APP接口--Bbs" )
@RestController
@ApiIgnore
@RequestMapping("rest/bbs")
public class BbsController {
    @Autowired
    private BbsService bbsService;

    /** 列表 */
    @ApiOperation(value="列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Bbs.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size",value = "数量",paramType = "body"),
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        List<Bbs> list = bbsService.latest(params);
        return R.ok().put("data", list);
    }

    @ApiOperation(value="头条")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Bbs.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size",value = "数量",paramType = "body"),
    })
    @GetMapping("/top")
    public R top(){
        Bbs bbs = bbsService.top();
        return R.ok().put("data", bbs);
    }

    /** 保存 */
    @ApiOperation(value="添加")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content",value = "内容",paramType = "body"),
            @ApiImplicitParam(name = "top",value = "是否头条 1是，0否",paramType = "body"),
    })
    @PostMapping("/save")
    public R save(@RequestBody Bbs bbs, @RequestAttribute("userId") Integer userId){
        bbs.setUserId(userId);
        bbsService.add(bbs);
        return R.ok();
    }

}